#!/usr/bin/python

"""
Pacmon, an updates notification tray application for the users of Arch Linux
Copyright 2006 Matt McDonald <metzen@gmail.com>

This file is part of Pacmon.

Pacmon is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Pacmon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""

import pynotify
import pygtk
pygtk.require('2.0')
import gtk


class PacmonNotification:
    def __init__(self, title, parent, show_cb, install_cb):
        """Construct a PacmonNotification

        title -- the title on the Notification
        parent -- the status icon to attach the popup to
        show_cb -- a callback function for the Show Updates button
        install_cb -- callback function for the Install Updates button
        """

        pynotify.init("pacmon")
        self.title = title
        self.n = pynotify.Notification(title)
        #icon = gtk.gdk.pixbuf_new_from_file('/usr/share/pacmon/images/pacman.svg')
        #self.icon = icon.scale_simple(32,32,gtk.gdk.INTERP_BILINEAR)
        #self.n.set_icon_from_pixbuf(self.icon)
        self.n.attach_to_status_icon(parent)
        self.n.set_timeout(15000) # 15 second timeout
        def cb(x,y):
            print "gotcha"
            show_cb(x)
            print "showed"
        self.n.add_action("Show Updates", "Show Updates", lambda x,y: show_cb(x))
        self.n.add_action("Install Updates", "Install Updates",
                          lambda x,y: install_cb(x))
        def close_cb(obj, junk):
            pass
        self.n.connect("closed", close_cb, "closed")

    def show_popup(self, text):
        """Show a popup with the specified body text"""
        self.n.update(self.title, text, "system-software-update")
        # n.update also modifies the icon, so we need to reset it
        #self.n.set_icon_from_pixbuf(self.icon)
        gtk.gdk.threads_enter()
        try:
            self.n.show()
        except:
            # couldn't popup, oh well
            print "ERROR--> Popup display failed!"
            print "ERROR--> Is 'notification-daemon' or "+\
                  "'notification-daemon-xfce' running?"
        gtk.gdk.threads_leave()
